package com.lock.reentrantLock;


import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @PackageName:com.lock.reentrantLock
 * @ClassName:Test03
 * @Description: ReentrantLock锁的可重入性 可重入锁指可以犯法获得该锁
 * @Author: zhangxiaoyu
 * @date 2021/1/19 16:23
 */
public class Test03 {

    public static void main(String[] args) throws InterruptedException {
        SubThread t1 = new SubThread();
        SubThread t2 = new SubThread();

        t1.start();
        t2.start();

        t1.join();
        t2.join();

        System.out.println(SubThread.num);

    }


    static class SubThread extends Thread {
         //定义锁对象
         private static Lock lock = new ReentrantLock();
         //定义变量
         public static int num = 0;

        @Override
        public void run() {
            for (int i = 0;i<10000;i++){
                try {
                    lock.lock();
                    num++;
                }  finally {
                    lock.unlock();
                }

            }
        }
    }
}
